import pandas, duckdb
from config import ini_op
from openpyxl import  load_workbook
import os
from utils.path_utils import get_project_root
from database.duckdb.duckdb_base import get_connection



# timenow = str(strftime("%Y%m%d%H%M%S"))
def run(dwbh, zbid, zb, kjnd, timenow):
  conn = get_connection()

  conn.execute("""
               delete from T_API_FZNC where kjnd = ? and LEDGERBH = ? and ACCTITLECODE in (select kmdm from GL_Fzye group by kmdm);
               """, [kjnd, zb])
  conn.execute("""
    insert into T_API_FZNC (ACCORGBH, LEDGER, LEDGERBH, ACCTITLECODE, SPECATEBH02, BALANCEDIR, BEGINBALANCEAMT, CURCUMDRAMT, CURCUMCRAMT, esun_api_update, kjnd)
              select
                '"""+dwbh+"""',
                '"""+zbid+"""',
                '"""+zb+"""',
                kmdm,
                fzdm1,
                CASE 
                    WHEN Ncj > Ncd THEN 1
                    WHEN Ncj < Ncd THEN 2
                    ELSE 0
                end,
                CAST(ABS(Ncj - Ncd) AS DECIMAL(18, 2)),
                CAST(0 AS DECIMAL(18, 2)),
                CAST(0 AS DECIMAL(18, 2)),
                '"""+timenow+"""',
                '"""+kjnd+"""'
               from GL_Fzye;
    delete from T_API_FZNC where ACCTITLECODE = '合计';
  """)

  conn.close()

